﻿using System;
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Controls;
using System.Windows.Input;
using myLiveMesh.Views.WindowManager;

namespace myLiveMesh.Behavior.WindowManager
{
    public class resizeRigthTopBehavior : Behavior<FrameworkElement>
    {
        Point _startDrag;

        public resizeRigthTopBehavior() { }

        protected override void OnAttached()
        {
            base.OnAttached();
            this.AssociatedObject.MouseLeftButtonDown += this.mouseLeftDownOnBorder;
            this.AssociatedObject.MouseLeftButtonUp += this.mouseLeftUpOnBorder;
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
        }

        private void mouseLeftDownOnBorder(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement side = sender as FrameworkElement;

            if (side != null)
            {
                Grid grid = side.Parent as Grid;
                if (grid != null)
                {
                    windowView win = grid.Parent as windowView;

                    win.Height = win.ActualHeight;
                    win.Width = win.ActualWidth;
                    win.pannelRec.Width = win.pannelRec.ActualWidth;
                    this._startDrag = e.GetPosition(win);
                    side.CaptureMouse();
                    this.AssociatedObject.MouseMove += mouseMoveOnBorder;

                }
            }
        }

        private void mouseLeftUpOnBorder(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement side = sender as FrameworkElement;

            if (side != null)
            {
                Grid grid = side.Parent as Grid;

                if (grid != null)
                {
                    windowView win = grid.Parent as windowView;

                    side.ReleaseMouseCapture();
                    this.AssociatedObject.MouseMove -= mouseMoveOnBorder;
                }
            }
        }

        private void mouseMoveOnBorder(object sender, MouseEventArgs e)
        {
            FrameworkElement side = sender as FrameworkElement;

            if (side != null)
            {
                Grid grid = side.Parent as Grid;

                if (grid != null)
                {
                    windowView win = grid.Parent as windowView;

                    if (win != null)
                    {
                        Point point = e.GetPosition(win);
                        double deltaY = this._startDrag.Y - point.Y;
                        double deltaX = point.X - this._startDrag.X;

                        this._startDrag = point;
                        win.Height += deltaY;
                        win.Width += deltaX;
                        win.pannelRec.Width += deltaX;
                        Canvas.SetTop(win, Canvas.GetTop(win) - deltaY);

                    }
                }
            }
        }

    }
}
